package com.funky.controller;


import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.funky.common.annotation.UserLoginToken;
import com.funky.common.entity.Result;
import com.funky.common.entity.StatusCode;
import com.funky.common.util.JWTutil;
import com.funky.pojo.Number;
import com.funky.pojo.User;
import com.funky.service.NumberService;
import com.funky.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author xiaojin
 * @since 2021-01-08
 */
@RestController
@RequestMapping("/number")
@CrossOrigin
@Api(tags = "会员购买管理相关接口")
public class NumberController {

  @Autowired
  private NumberService numberService;

  @Autowired
  private UserService userService;

  @Autowired
  private JWTutil jwTutil;

  @RequestMapping(value = "buy/{month}", method = RequestMethod.POST)
  @ApiOperation("购买会员的接口")
  @ApiImplicitParam(name = "month", value = "月数", required = true, paramType = "path", dataType = "Integer")
  @UserLoginToken
  public Result buy(HttpServletRequest request, @PathVariable Integer month){
    //通过请求头的token解析出当前登录用户的id，用于创建人id
    String token = request.getHeader("token");
    User user = userService.findUserById(JWTutil.getId(token));
    if (ObjectUtil.isNotEmpty(user)){
      Integer flag = numberService.addNumber(user, month);
      if(flag == 1){
        return new Result(true, StatusCode.OK, "购买成功",null);
      }else {
        return new Result(false, StatusCode.ERROR, "出错了",null);
      }
    }
    return new Result(false, StatusCode.ERROR, "出错了",null);
  }


  @RequestMapping(value = "skipAd", method = RequestMethod.GET)
  @ApiOperation("跳过广告的接口")
  public Result buy(HttpServletRequest request){
//通过请求头的token解析出当前登录用户的id，用于创建人id
    String token = request.getHeader("token");
    if (token == null){
      return new Result(false, StatusCode.LOGINERROR, "请先登录",null);
    }
    User user = userService.findUserById(JWTutil.getId(token));
    //判断用户会员是否过期
    isNumber(user);
    //不过期
    if(user.getMember()==1){
      return new Result(true, StatusCode.OK, "跳过成功",null);
    }
    return new Result(false, StatusCode.ERROR, "您不是会员",null);
  }

  //判断用户会员是否过期
  public void isNumber(User user) {
    if (user.getMember() == 1) {
      String memberId = user.getMemberId();
      Number number = numberService.getById(memberId);
      if (number.getEndTime().getTime() < new Date().getTime()) {
        //说明会员过期了，清除会员数据
        user.setMember(0);
        user.setMemberId("");
        UpdateWrapper<User> updateWrapper = new UpdateWrapper();
        userService.update(user,updateWrapper.eq("id", user.getId()));
        numberService.removeById(memberId);
      }
    }
  }
}
